class Solution {
    public int lengthOfLongestSubstring(String s) {
         Deque<Character> deque = new ArrayDeque<>();
        int max = 0;
        for(int  i = 0;i < s.length();i++) {
            if(!deque.contains(s.charAt(i))) {
                deque.add(s.charAt(i));
            } else {
                max = Math.max(max,deque.size());
                while(deque.contains(s.charAt(i))) {
                    deque.pop();
                }
                deque.add(s.charAt(i));
            }
        }
        max = Math.max(max,deque.size());
        return max;
    }
}